package com.lcc.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lcc.gulimall.product.entity.AttrEntity;
import com.lcc.gulimall.product.service.AttrAttrgroupRelationService;
import com.lcc.gulimall.product.service.AttrService;
import com.lcc.gulimall.product.service.CategoryService;
import com.lcc.gulimall.product.vo.AttrGroupDescVo;
import com.lcc.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lcc.gulimall.product.entity.AttrGroupEntity;
import com.lcc.gulimall.product.service.AttrGroupService;
import com.lcc.common.utils.PageUtils;
import com.lcc.common.utils.R;



/**
 * 属性分组
 *
 * @author Yunmo
 * @email li_cuican@163.com
 * @date 2021-11-03 22:08:52
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取分类下所有分组&关联规格参数
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R attrgroupCateWithAttr(@PathVariable("catelogId")Long catelogId ){
        List<AttrGroupDescVo> vos = attrGroupService.listAttrGroupWithAttrByCatId(catelogId,1);
        return R.ok().put("data", vos);
    }
    /**
     * 新增关联关系
     */
    @PostMapping("/attr/relation")
    public R attrAttrgroupRelation(@RequestBody List<AttrGroupRelationVo> relationVos){
        relationService.saveBatchRelation(relationVos);
        return R.ok();
    }
    /**
     * 根据组内ID找到该分组下的所有基本属性
     * @param attrgroupId
     * @return
     */
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entityList);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrGroupService.deleteRelation(vos);
        return R.ok();
    }
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", pageUtils);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		Long[] categoryPath = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(categoryPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
